package dm71.espil.activities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

import dm71.espil.FriendsListAdaptor;
import dm71.espil.PendingRequestsAdapter;
import dm71.espil.R;
import dm71.espil.REST.DataService;
import dm71.espil.REST.Tasks.LoadFriendsTask;
import dm71.espil.REST.Tasks.LoadPendingFriendRequestsTask;
import dm71.espil.models.PendingRequest;
import dm71.espil.models.User;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class FriendListActivity extends ListActivity
{
	FriendsListAdaptor adapter;
	FriendListActivity context = this;
	User[] users;
	PendingRequest[] pending;
	
	ListView pendingFriendInvites;
	DataService ds;
	
	TextView txtFriends;
	TextView txtPendingRequests;

	public void onCreate(Bundle icicle)
	{
		super.onCreate(icicle);
		setContentView(R.layout.friendlist);
		
		txtFriends = (TextView)findViewById(R.id.txtFriends);
		txtPendingRequests = (TextView)findViewById(R.id.txtPendingFriendRequests);
		
		ds = new DataService();
		
		Load();
	}
	
	private void Load()
	{
		LoadFriendsTask loadFriends = new LoadFriendsTask(context);
		loadFriends.execute(0);
		
		LoadPendingFriendRequestsTask loadPendingRequests = new LoadPendingFriendRequestsTask(context);
		loadPendingRequests.execute(0);
	}

	public void LoadFriendsCompleted(User[] users)
	{
		this.users = users;
		
		adapter = new FriendsListAdaptor(this, R.layout.friendlistitem, users);
		setListAdapter(adapter);
		
		//If any friends show friends
		txtFriends.setVisibility(users.length > 0 ? TextView.VISIBLE : TextView.INVISIBLE);
	}

	public void LoadPendingInvitesCompleted(PendingRequest[] pending)
	{
		pendingFriendInvites = (ListView) findViewById(R.id.pendingFriendsList);
		this.pending = pending;

		PendingRequestsAdapter pendingAdaptor = new PendingRequestsAdapter(this, R.layout.pendingfriendrequestitem, pending);
		pendingFriendInvites.setAdapter(pendingAdaptor);
		
		//If any pending invites show label
		txtPendingRequests.setVisibility(pending.length > 0 ? TextView.VISIBLE : TextView.INVISIBLE);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		User user = users[position];
		Intent i = new Intent(this, VisProfilActivity.class);
		Bundle p = new Bundle();
		p.putSerializable("user", (Serializable) user);
//		p.putString("name", user.getName());
//		p.putString("lastname", user.getLastName());
//		p.putString("username", user.getUsername());
//		p.putString("klasse", user.getKlasse());
//		p.putString("region", user.getRegion());
		i.putExtra("android.intent.extra.INTENT", p);
		startActivity(i);
	}
	
	public void AcceptFriendRequestClicked(View v)
	{
		int index = GetIndexOfView(v);
		PendingRequest pendingReq = this.pending[index];
		ds.AcceptFriendRequest(pendingReq.getUser().getUserID());
		
		//refresh everything
		Load();
	}
	
	public void DenyFriendRequestClicked(View v)
	{
		int index = GetIndexOfView(v);
		PendingRequest pendingReq = this.pending[index];
		ds.Unfriend(pendingReq.getUser().getUserID());
		
		LoadPendingFriendRequestsTask loadPendingRequests = new LoadPendingFriendRequestsTask(context);
		loadPendingRequests.execute(0);
	}
	
	private int GetIndexOfView(View v)
	{
		View root = (View) v.getParent();
		return pendingFriendInvites.indexOfChild(root);
	}
	
	public void chat(View v)
	{
		int index = GetIndexOfView(v);
		Toast.makeText(context, "Her kommer chat", 1000).show();
	}
	
	public void throwbanana(View v)
	{
		int index = GetIndexOfView(v);
		Toast.makeText(context, "Du har kastet en banan (TODO)", 1000).show();
	}
}